package org.jeecg.modules.xindian.reconciliation.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.xindian.reconciliation.entity.SalesmanManagerRate;
import org.jeecg.modules.xindian.reconciliation.enums.DeleteFlagEnum;
import org.jeecg.modules.xindian.reconciliation.mapper.SalesmanManagerRateMapper;
import org.jeecg.modules.xindian.reconciliation.service.ISalesmanManagerRateService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 业务员结算比例
 * @Author: jeecg-boot
 * @Date: 2024-04-11
 * @Version: V1.0
 */
@Service
public class SalesmanManagerRateServiceImpl extends ServiceImpl<SalesmanManagerRateMapper, SalesmanManagerRate> implements ISalesmanManagerRateService {

    public Boolean deleteSalesmanManagerRate(String id) {
        return lambdaUpdate().set(SalesmanManagerRate::getDeleteFlag, DeleteFlagEnum.Y).eq(SalesmanManagerRate::getId, id).update();
    }


    public Boolean deleteSalesman(String id) {
       return getBaseMapper().deleteBySalesmanId(id)>0;
    }

    @Override
    public List<SalesmanManagerRate> queryBySalesmanId(String salesmanId) {
        return lambdaQuery().eq(SalesmanManagerRate::getDeleteFlag, DeleteFlagEnum.N).eq(SalesmanManagerRate::getSalesmanId, salesmanId).list();
    }

    @Override
    public boolean save(SalesmanManagerRate entity) {
        checkData(entity);
        entity.setDeleteFlag(DeleteFlagEnum.N);
        return super.save(entity);
    }

    @Override
    public List<SalesmanManagerRate> listAllEnableRate() {
        return lambdaQuery().eq(SalesmanManagerRate::getDeleteFlag, DeleteFlagEnum.N).list();
    }

    /**
     * 校验数据
     *
     * @param data
     */
    private void checkData(SalesmanManagerRate data) {
        Assert.notNull(data.getSalesmanId(), "业务员id不能为空");
        Assert.notNull(data.getFixRate(), "固定结算比例不能为空");
        Assert.notNull(data.getCompany(), "客户名称不能为空");
        //判断客户是否存在
        LambdaQueryChainWrapper<SalesmanManagerRate> query = lambdaQuery().eq(SalesmanManagerRate::getCompany, data.getCompany()).eq(SalesmanManagerRate::getDeleteFlag, DeleteFlagEnum.N);
        if (StringUtils.isNotBlank(data.getId())) {
            query.ne(SalesmanManagerRate::getId, data.getId());
        }
        Long count = query.count();
        Assert.isTrue(count == 0L, data.getCompany() + "已经被配置");
        InsuranceSettlementConfigServiceImpl.checkBetween(data.getFixRate(), BigDecimal.ZERO, BigDecimal.ONE, "固定结算比例");
        List<SalesmanManagerRate.OtherAreaRateConfig> areaRateConfigs = data.getConfigs();
        if (CollectionUtils.isNotEmpty(areaRateConfigs)) {
            areaRateConfigs.forEach(c -> {
                Assert.notNull(c.getRate(), "费率不能为空");
                Assert.notEmpty(c.getPlatform(), "平台代码不能为空");
                Assert.notBlank(c.getInsuranceCompany(), "保险公司不能为空");
                InsuranceSettlementConfigServiceImpl.checkBetween(c.getRate(), BigDecimal.ZERO, BigDecimal.ONE, "费率值");
            });
        }
        data.buildConfigString();
        Assert.notNull(data.getSalesmanId(), "业务员id不能为空");
    }

    @Override
    public boolean updateById(SalesmanManagerRate entity) {
        checkData(entity);
        return super.updateById(entity);
    }
}
